package cn.tedu.egmvc3.controller;

import cn.tedu.egmvc3.base.response.JsonResult;
import cn.tedu.egmvc3.mapper.UserMapper;
import cn.tedu.egmvc3.pojo.dto.UserAddParam;
import cn.tedu.egmvc3.pojo.dto.UserUpdateParam;
import cn.tedu.egmvc3.pojo.entity.User;
import cn.tedu.egmvc3.pojo.vo.UserListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

@RequestMapping("/v1/user/")
@RestController
@Slf4j
@Api(tags = "用户控制模块")
@Validated
public class UserController {
    @Autowired
    public UserMapper userMapper;

    /**
     *  添加用户
     * @param userAddParam
     * @return
     */
    @PostMapping("add")
    @ApiOperation("添加用户")
    @ApiOperationSupport(order = 10)
    public JsonResult UserAdd(@Validated UserAddParam userAddParam){
        log.debug("userAddParam"+userAddParam);
        User user = new User();
        BeanUtils.copyProperties(userAddParam,user);
        user.setCreateTime(new Date());

        log.debug("user:"+user);
        userMapper.insertUser(user);

        return JsonResult.success();
    }

    /**
     *  查询用户列表
     * @param userListVO
     * @return
     */
    @GetMapping("list")
    @ApiOperation("查询用户")
    @ApiOperationSupport(order = 20)
    public JsonResult userListVOS(UserListVO userListVO){
        log.debug("userListVO"+userListVO);
        String username = userListVO.getUsername();
        String password = userListVO.getPassword();

        log.debug("userListVO"+userListVO);
        List<UserListVO> userListVOS = userMapper.selectUser(username, password);

        return JsonResult.success(userListVOS);
    }

    /**
     * 删除指定用户
     * @param id
     * @return
     */
    @PostMapping("del")
    @ApiOperation("删除指定用户")
    @ApiOperationSupport(order = 30)
    @ApiImplicitParam(name = "id",value = "用户编号",required = true,dataType = "int")
    public JsonResult delectUser(@Range(min = 1,max = 100,message = "用户ID必须在1到100之间")@RequestParam Integer id){
        if(id <0){
            throw new IllegalArgumentException("用户id不能小于0");
        }
        log.debug("id = " + id);
        userMapper.delectUser(id);

        return JsonResult.success();
    }

    /**
     * 更新指定用户
     * @param userUpdateParam
     * @return
     */
    @PostMapping("update")
    @ApiOperation("更新指定用户")
    @ApiOperationSupport(order = 40)
    public JsonResult updateUser(@ApiIgnore UserUpdateParam userUpdateParam){
        log.debug("userUpdateParam"+userUpdateParam);
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam,user);
        log.debug("user:"+user);
        userMapper.updateUser(user);

        return JsonResult.success();
    }
}
